﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BGSpaceShooterServer;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ParticleSystem;

namespace BGSpaceShooter
{
    public class StarFighter : GameObject
    {
        public List<StarFighterComponent> StarFighterComponents { get; set; }
        public Vector3 Acceleration { get; set; }
        public Vector3 MaxVelocity { get; set; }
        public int ModelID { get; set; }

        public Dictionary<EngineName, ParticleSystem.IObjectEffect> EngineEffects { get; set; }

        protected List<EngineName> workingEngines;

        public List<EngineName> WorkingEngines
        {
            get { return workingEngines; }
            set
            {
                workingEngines = value;
                foreach (KeyValuePair<EngineName, IObjectEffect> engineEffect in EngineEffects)
                {
                    engineEffect.Value.IsActive = false;
                }

                foreach (EngineName engineName in value)
                {
                    switch (engineName)
                    {
                        case EngineName.leftZ:
                        case EngineName.rightZ:
                        case EngineName.topX:
                        case EngineName.bottomX:
                        case EngineName.Main:
                            EngineEffects[engineName].IsActive = true;
                            break;
                        default:
                            break;
                    }
                }
            }
        }


        public StarFighter(Model model)
            : base(model)
        {
            StarFighterComponents = new List<StarFighterComponent>();
            foreach (ModelMesh modelMesh in Model.Meshes)
            {
                StarFighterComponent fighterComponent = new StarFighterComponent(this);

                fighterComponent.MeshName = modelMesh.Name;
                fighterComponent.BoundingSphere = modelMesh.BoundingSphere;

                StarFighterComponents.Add(fighterComponent);
            }

            
            EngineEffects = new Dictionary<EngineName, IObjectEffect>();

            EngineEffects.Add(EngineName.Main, new ParticleSystem.WaterEffect(this));
            EngineEffects.Add(EngineName.leftZ, new ParticleSystem.SteamEffect(this, EngineName.leftZ));
            EngineEffects.Add(EngineName.rightZ, new ParticleSystem.SteamEffect(this, EngineName.rightZ));
            EngineEffects.Add(EngineName.topX, new ParticleSystem.SteamEffect(this, EngineName.topX));
            EngineEffects.Add(EngineName.bottomX, new ParticleSystem.SteamEffect(this, EngineName.bottomX));
        }

        public override IEnumerable<ParticleSystem.IObjectEffect> GetEffectsToDraw()
        {
            var engineEffects = from effect in EngineEffects select effect.Value;
            var componentEffectsLists = from component in StarFighterComponents select component.GetEffects();
            var effectsInComponents = componentEffectsLists.Aggregate((working, next) => working.Concat(next));
            return engineEffects.Concat(effectsInComponents);
        }


        public float HealthPercent
        {
            get
            {
                return (float)HP / (float)MaxHP;
            }
        }

        public override IDrawer GetDrawer()
        {
            return new StarFighterDrawer(this);
        }


    }
}
